package com.tms.app.tms.ui;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.sdk.android.ams.common.util.StringUtil;
import com.bigkoo.pickerview.TimePickerView;
import com.tms.app.tms.R;
import com.tms.app.tms.app.Constant;
import com.tms.app.tms.bean.WaybillInfo;
import com.tms.app.tms.bean.response.BaseResponse;
import com.tms.app.tms.ui.base.ui.BaseListActivity;
import com.tms.app.tms.utils.TimeUtil;
import com.tms.app.tms.widget.CommonDialog;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.xutils.http.RequestParams;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.tms.app.tms.utils.TimeUtil.DEFAULT_DATE_FORMAT;
import static com.tms.app.tms.utils.TimeUtil.getTime;

/**
 * Created by Administrator on 2017/10/28 0028.
 */

public class WayBillInfoAC extends BaseListActivity {
    WBillPresenter presenter = new WBillPresenter();
    @BindView(R.id.text1)
    TextView text1;
    @BindView(R.id.text2)
    TextView text2;
    @BindView(R.id.text3)
    TextView text3;
    @BindView(R.id.text4)
    TextView text4;
    @BindView(R.id.text5)
    TextView text5;
    @BindView(R.id.text6)
    TextView text6;
    @BindView(R.id.text7)
    TextView text7;
    @BindView(R.id.text8)
    TextView text8;
    @BindView(R.id.text9)
    TextView text9;
    @BindView(R.id.text10)
    TextView text10;
    @BindView(R.id.text11)
    TextView text11;
    @BindView(R.id.text12)
    TextView text12;
    @BindView(R.id.button_send)
    Button buttonSend;
    @BindView(R.id.layout_info)
    LinearLayout layoutInfo;
    @BindView(R.id.text_next)
    TextView textNext;
    private int dispatchId;
    private String adress;
    private int type = 0;// 2 出口
    private int planIdShowType;
    private CommonDialog commonDialog;
    private String dialogTitle = "确定要发送预录内容吗";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_layout_waybill_info);
        ButterKnife.bind(this);
        setToolBarTitle("运单详情", -1, -1);
        dispatchId = getIntent().getIntExtra("dispatchId", 0);
        planIdShowType = getIntent().getIntExtra("planIdShowType", -1);
        if (dispatchId == 0) {
            showMsg("无此运单");
            finish();
        }
        type = getIntent().getIntExtra("type", 0);
        adress = getIntent().getStringExtra("adress");
        presenter.setiView(this);
        presenter.waybillDetail(token, dispatchId);

    }

    @Override
    protected void onStart() {
        super.onStart();
        if (type == 1) {
//            text1.setEnabled(true);
//            text2.setEnabled(true);
//            text3.setEnabled(true);
//            text4.setEnabled(true);
//            text5.setEnabled(true);
//            text6.setEnabled(true);
//            text7.setEnabled(true);
//            text8.setEnabled(true);
//            text9.setEnabled(true);
//            text10.setEnabled(true);
//            text11.setEnabled(true);
//            text12.setEnabled(true);
            text1.setEnabled(false);
            text2.setEnabled(false);
            text3.setEnabled(false);
            text4.setEnabled(false);
            text5.setEnabled(false);
            text6.setEnabled(false);
            text7.setEnabled(false);
            text8.setEnabled(false);
            text9.setEnabled(false);
            text10.setEnabled(false);
            text11.setEnabled(false);
            text12.setEnabled(true);
            textNext.setVisibility(View.VISIBLE);
            buttonSend.setVisibility(View.VISIBLE);
        } else {
            text1.setEnabled(false);
            text2.setEnabled(false);
            text3.setEnabled(false);
            text4.setEnabled(false);
            text5.setEnabled(false);
            text6.setEnabled(false);
            text7.setEnabled(false);
            text8.setEnabled(false);
            text9.setEnabled(false);
            text10.setEnabled(false);
            text11.setEnabled(false);
            text12.setEnabled(false);
            buttonSend.setVisibility(View.GONE);
            textNext.setVisibility(View.INVISIBLE);
        }
        if (planIdShowType == 3 || planIdShowType == 13) {
            text1.setEnabled(false);
            text2.setEnabled(false);
            text3.setEnabled(false);
            text4.setEnabled(false);
            text5.setEnabled(false);
            text6.setEnabled(false);
            text7.setEnabled(false);
            text8.setEnabled(false);
            text9.setEnabled(false);
            text10.setEnabled(false);
            text11.setEnabled(false);
            text12.setEnabled(false);
            buttonSend.setVisibility(View.GONE);
            textNext.setVisibility(View.INVISIBLE);
        }
    }

    @OnClick({R.id.button_send, R.id.layout_info})
    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v.getId() == R.id.button_send) {
            commonDialog = CommonDialog.getInstance(this, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    saveData(v);
                    commonDialog.close();
                }
            }, dialogTitle);
            commonDialog.show();
        }
        if (v.getId() == R.id.layout_info && type == 1) {
            Intent intent = new Intent(this, GoodsInfoActivity.class);
            intent.putExtra("dispatchId", dispatchId);
            startActivityForResult(intent, 0x512);
        }

    }

    /******8
     * 获取运单详情
     * @param info
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void bindView(WaybillInfo info) {
        text1.setText(info.getWaybillNo());

        switch (info.getBusiType()) {
            case "0":
                text2.setText("进口");
                break;
            case "1":
                text2.setText("出口");
                break;
            case "2":
                text2.setText("公路");
                break;
            default:
                text2.setText("");
        }
        // text2.setText(info.getBusiType());
        text3.setText(info.getBillNo());
        text4.setText(info.getShipName());
        text5.setText(info.getVoy_name());
        text6.setText(TimeUtil.getTimeFormat(info.getOpenShipTime()));
        text7.setText(TimeUtil.getTimeFormat(info.getEndShipTime()));
        text8.setText(info.getContainerTypeName());
        text9.setText(info.getContainerNo());
        text10.setText(info.getSealNo());
        text11.setText(info.getGoodsName());
        if (info.getPiece() != null )
            text12.setText(info.getPiece() + "/" + info.getWeight() + "/" + info.getVolume());

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void bindView(BaseResponse baseResponse) {
        if (baseResponse.getCode() == 200 && baseResponse.getRequestType() == 2) {
            showMsg("保存成功");
            finish();
        } else {
            if (baseResponse.getRequestType() == 2)
                showMsg(baseResponse.getError());
        }

    }


    //@OnClick({R.id.button_send})
    public void saveData(View view) {
        List<TextView> list = new ArrayList<>();
        list.add(text1);
        list.add(text2);
        list.add(text3);
        list.add(text4);
        list.add(text5);
        list.add(text6);
        list.add(text7);
        list.add(text8);
        list.add(text9);
        list.add(text10);
        list.add(text11);
        list.add(text12);
//        for (TextView textView : list) {
//            if (StringUtil.isEmpty(textView.getText().toString())) {
//                showMsg("请把内容填写完整");
//                return;
//            }
//        }
        RequestParams params = new RequestParams(Constant.prececord);
        params.addBodyParameter("address", adress);
        params.addBodyParameter("shipName_voyNo", text4.getText().toString());//船名
        params.addBodyParameter("voyNo", text5.getText().toString());//航次
        params.addBodyParameter("contNo", text9.getText().toString());//箱号
        params.addBodyParameter("sealNo", text10.getText().toString());//封号
        params.addBodyParameter("contSize", text8.getText().toString());//箱型
        params.addBodyParameter("arrbillNo", text3.getText().toString());//提单号
        params.addBodyParameter("arrGoodsDescription", text11.getText().toString());//货名
        params.addBodyParameter("openShipTime", text6.getText().toString());
        params.addBodyParameter("endShipTime", text7.getText().toString());
        params.addBodyParameter("remark", text12.getText().toString());
        params.addBodyParameter("dispatchId", dispatchId + "");
        params.addBodyParameter("token", token);
        presenter.saveRecode(params);
    }


    @OnClick({R.id.text6, R.id.text7})
    public void onMyClick(View v) {
        TextView textView = (TextView) v;
        pickTime(textView);
    }

    public void pickTime(final TextView textView) {
        //时间选择器
        Calendar selectedDate = Calendar.getInstance();
        Calendar startDate = Calendar.getInstance();
        //startDate.set(2013,1,1);
        Calendar endDate = Calendar.getInstance();
        //endDate.set(2020,1,1);

        //正确设置方式 原因：注意事项有说明
        startDate.set(1990, 0, 1);
        // endDate.set(2020,11,31);

        TimePickerView pvTime = new TimePickerView.Builder(this, new TimePickerView.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {//选中事件回调
                textView.setText(getTime(date, DEFAULT_DATE_FORMAT));
            }
        })
                .setType(new boolean[]{true, true, true, true, true, true})// 默认全部显示
                .setCancelText("取消")//取消按钮文字
                .setSubmitText("确认")//确认按钮文字
                .setContentSize(18)//滚轮文字大小
                .setTitleSize(20)//标题文字大小
                // .setTitleText("Title")//标题文字
                .setOutSideCancelable(true)//点击屏幕，点在控件外部范围时，是否取消显示
                .isCyclic(true)//是否循环滚动
                .setTitleColor(Color.BLACK)//标题文字颜色
                .setSubmitColor(Color.BLUE)//确定按钮文字颜色
                .setCancelColor(Color.BLUE)//取消按钮文字颜色
                .setTitleBgColor(0xFF0081C2)//标题背景颜色 Night mode
                .setBgColor(0xFF0081C2)//滚轮背景颜色 Night mode
                .setDate(selectedDate)// 如果不设置的话，默认是系统时间*/
                .setRangDate(startDate, endDate)//起始终止年月日设定
                // .setLabel("年", "月", "日", "时", "分", "秒")//默认设置为年月日时分秒
                .setLabel("年", "月", "日", "时", "分", "秒")//默认设置为年月日时分秒
                .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                .isDialog(true)//是否显示为对话框样式
                .build();
        pvTime.setDate(Calendar.getInstance());//注：根据需求来决定是否使用该方法（一般是精确到秒的情况），此项可以在弹出选择器的时候重新设置当前时间，避免在初始化之后由于时间已经设定，导致选中时间与当前时间不匹配的问题。
        pvTime.show();
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode==0x512){
            presenter.waybillDetail(token, dispatchId);
        }
        presenter.waybillDetail(token, dispatchId);
    }
}
